package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.FeedbackService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.app.Feedback;
import com.iot08.common.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Weichao
 * @Date: 2024/09/14/17:36
 * @Description:
 */
@Slf4j
@Controller
@RequestMapping("feedback")
public class FeedbackController extends BaseController {
    @Resource
    FeedbackService feedbackService;


    @RequestMapping("list")
    public ModelAndView list(PageVO page,
                             @RequestParam(value = "searchContent", required = false) String searchContent,
                             @RequestParam(value = "pass", required = false) Integer pass) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("feedback/feedback_list");
        Page<Feedback> pages = feedbackService.list(page, searchContent, pass);

        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("feedbackList", pages.getContent());
        mv.addObject("searchContent", searchContent);
        mv.addObject("pass", pass);
        mv.addObject("pageStr", page.getPageStr());

        mv.addObject("butSet", getMyBut(page.getMenuId()));

        return mv;

    }

    //changePass

    @ResponseBody
    @RequestMapping("changePass")
    public Response<Object> changePass(@RequestParam(value = "id") String id,
                                       @RequestParam(value = "pass") Integer pass,
                                       @RequestParam(value = "tm") String tm,
                                       @RequestParam(value = "menuId") String menuId) {
        log.info("删除 APP 用户 : {} 操作时间:{}", id, tm);
        //检查有无操作按钮权限
        Set<String> butSet = getMyBut(menuId);
        if(!butSet.contains("pass")){
            return Response.failedResponse("没有权限");
        }
        return feedbackService.changePass(id, pass, getSessionUser(), getRemortIP());
    }
    @ResponseBody
    @RequestMapping("del")
    public Response<Object> del(@RequestParam(value = "id") String id,
                                @RequestParam(value = "tm") String tm,
                                @RequestParam(value = "menuId") String menuId) {
        log.info("删除 反馈记录 : {} 操作时间:{}", id, tm);
        //检查有无操作按钮权限
        Set<String> butSet = getMyBut(menuId);
        if(!butSet.contains("del")){
            return Response.failedResponse("没有权限");
        }
        return feedbackService.del(id, getSessionUser(), getRemortIP());
    }
}
